<?PHP
class ConnectionMysql
{
    private $connection = NULL;
    private $server = NULL;
    private $user = NULL;
    private $password = NULL;
    private $dataBase = NULL;


    /*************************************************************************
    * Name: __construct
    * Description: Construtor da Classe. Inicializar os Parâmentros.
    * Parameters: String, String, String, String
    * Returns: Nenhum
    * Author: Felipe O. Simões
    * Last Modified: 06/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function __construct($server = 'localhost', $user = 'root', $password = '', $dataBase = 'portalphp')
    {
        $this->server = $server;
        $this->user = $user;
        $this->password = $password;
        $this->dataBase = $dataBase;
    }


    /*************************************************************************
    * Name: connect()
    * Description: Realiza a Conexão com o Servidor e com o DataBase.
    * Parameters: Nenhum
    * Returns: Nenhum
    * Author: Felipe O. Simões
    * Last Modified: 06/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function connect()
    {
        $this->connection = mysql_connect($this->server, $this->user, $this->password); //or die('Erro ao Conectar no Servidor');
        mysql_select_db($this->dataBase, $this->connection);// or die('Erro ao Conectar no DataBase');
    }


    /*************************************************************************
    * Name: closeConnection()
    * Description: Fecha a Conexão com o Servidor.
    * Parameters: Nenhum
    * Returns: Nenhum
    * Author: Felipe O. Simões
    * Last Modified: 06/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function closeConnection()
    {
        mysql_close($this->connection);// or die('Erro ao Fechar a Conexão');
    }


    /*************************************************************************
    * Name: getConnect();
    * Description: Retorna a Conexão com o Servidor, caso exista.
    * Parameters: Nenhum
    * Returns: Connection
    * Author: Felipe O. Simões
    * Last Modified: 06/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function getConnect()
    {
        return $this->connection;
    }


    /*************************************************************************
    * Name: query()
    * Description: Recebe e Executa o comando SQL definido pelo Usuário.
    * Parameters: String
    * Returns: Result
    * Author: Felipe O. Simões
    * Last Modified: 06/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function query($sql)
    {
        return mysql_query($sql, $this->connection);// or die('Erro ao Executar o Comando SQL.');
    }


    /*************************************************************************
    * Name: queryConnect()
    * Description: - Realiza a Conexão com o Servidor e com o DataBase;
    *			   - Recebe e Executa o Comando SQL definido pelo Usuário;
    *			   - Fecha a Conexão com o Servidor;
    *			   - Retorna o Resultado do Comando SQL.
    * Parameters: String
    * Returns: Result
    * Author: Felipe O. Simões
    * Last Modified: 06/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function queryConnect($sql)
    {                    
        $this->connect();
        $resultSql = $this->query($sql);
        $this->closeConnection();

        return $resultSql;
    }


    /*************************************************************************
    * Name: toString()
    * Description: Mostra Todas as Informações sobre o Objeto de Conexão.
    * Parameters: Nenhum
    * Returns: String
    * Author: Felipe O. Simões
    * Last Modified: 06/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function toString()
    {
        $info = 'Server: '. $this->server. ' - User: '. $this->user .' - Password: '. $this->password .
                        ' - DataBase: '. $this->dataBase .' - Connection: '. $this->connection;
        return $info;
    }
}
?>